const app = getApp();
const util = require('../../utils/util.js')
const key = 'RVHBZ-XL3C4-ONWUP-DDAIW-EJ3G3-RVFI4'; //使用在腾讯位置服务申请的key
const referer = '本地有小程序'; //调用插件的app的名称
const location = '';
const category = '';
const chooseLocation = requirePlugin('chooseLocation');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        address: '',
        area_list: {},
        area_list_str: '',
        city_id: '',
        area_id: '',
        picker_lng: '',
        picker_lat: '',
        area_info: '',
        true_name: '',
        mob_phone: '',
        detail_address: '',
        refund_id: '',
    },
    onLoad: function (options) {
        util.share();
        this.setData({
            refund_id: options.refund_id
        })
    },
    onReady: function () {
        var self = this;
        wx.getStorage({
            key: 'key',
            success: (res) => {
                self.setData({
                    key: res.data,
                })
                wx.request({
                    url: app.globalData.ApiUrl + '/index.php?act=member_address&op=get_area',
                    data: {
                        key: res.data,
                    },
                    method: 'POST',
                    header: {
                        'content-type': 'application/x-www-form-urlencoded'
                    },
                    dataType: 'json',
                    success: (res) => {
                        if (res.data.code == 200) {
                            let area_list_str = '';
                            let result = res.data.datas;
                            for (var i in result) {
                                area_list_str += result[i]['area_name'] + '，'
                            }
                            self.setData({
                                area_list: result,
                                area_list_str: area_list_str.substring(0, area_list_str.length - 1)
                            })
                        }
                    }
                });
            }
        })
        if (self.data.addressid) {
            wx.request({
                url: app.globalData.ApiUrl + '/index.php?act=member_address&op=address_info',
                data: {
                    key: self.data.key,
                    address_id: self.data.addressid
                },
                method: 'POST',
                header: {
                    'content-type': 'application/x-www-form-urlencoded'
                },
                dataType: 'json',
                success: (res) => {
                    if (res.data.code == 200) {
                        var result = res.data.datas;
                        self.setData({
                            city_id: result.address_info.city_id,
                            area_id: result.address_info.area_id,
                            area_info: result.address_info.area_info,
                            address: result.address_info.address,
                            picker_lat: result.address_info.lat,
                            picker_lng: result.address_info.lng,
                            true_name: result.address_info.true_name,
                            mob_phone: result.address_info.mob_phone,
                            detail_address: result.address_info.detail_address,
                        })
                    }
                }
            });
        }


    },
    onShow() {
        var self = this;
        const location = chooseLocation.getLocation();
        let area_ids = [];
        let area_info = '';
        if (location) {
            for (var i in self.data.area_list) {
                if (location.address.indexOf(self.data.area_list[i]['area_name']) != -1) {
                    area_ids = self.data.area_list[i]['area_ids'].split('_');
                    area_info = self.data.area_list[i]['area_name']

                }
            }
            if (area_info) {
                self.setData({
                    area_info: area_info,
                    address: location.name,
                    picker_lng: location.longitude,
                    picker_lat: location.latitude,
                    city_id: area_ids['1'],
                    area_id: area_ids['2']
                })
            } else {
                self.setData({
                    address: '',
                    city_id: '',
                    area_id: '',
                    picker_lng: '',
                    picker_lat: '',
                    area_info: ''
                })
                wx.showToast({
                    title: '该地址不在上门取件范围内，请重新选择',
                    icon: 'none',
                    duration: 3000
                })
            }
        }
    },
    chooseLocation: function () {
        wx.navigateTo({
            url: `plugin://chooseLocation/index?key=${key}&referer=${referer}&location=${location}&category=${category}`
        });
    },
    formSubmit: function (e) {
        var self = this;
        var uri = app.globalData.ApiUrl + '/index.php?act=member_return&op=door_pick';
        var postData = {
            key: self.data.key,
            true_name: e.detail.value.true_name,
            mob_phone: e.detail.value.mob_phone,
            city_id: self.data.city_id,
            area_id: self.data.area_id,
            address: self.data.address,
            area_info: self.data.area_info,
            lat: self.data.picker_lat,
            lng: self.data.picker_lng,
            detail_address: e.detail.value.detail_address,
            refund_id: self.data.refund_id
        };
        wx.request({
            url: uri,
            data: postData,
            method: 'POST',
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            dataType: 'json',
            success: (res) => {
                if (res.data.code == 200) {
                    wx.showToast({
                        title: '申请成功',
                        icon: 'success',
                        duration: 1000,
                        success: function () {
                            setTimeout(function () {
                                wx.redirectTo({
                                    url: '../member_refund/member_refund'
                                })

                            }, 1000);
                        }
                    })
                } else {
                    wx.showToast({
                        title: res.data.datas.error,
                        icon: 'none',
                        duration: 3000
                    })
                }
            }
        });
    }
})